class BillingAccountsController < ApplicationController
  # GET /billing_accounts
  # GET /billing_accounts.xml
  def index
    @billing_accounts = BillingAccount.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @billing_accounts }
    end
  end

  # GET /billing_accounts/1
  # GET /billing_accounts/1.xml
  def show
    @billing_account = BillingAccount.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @billing_account }
    end
  end

  # GET /billing_accounts/new
  # GET /billing_accounts/new.xml
  def new
    @billing_account = BillingAccount.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @billing_account }
    end
  end

  # GET /billing_accounts/1/edit
  def edit
    @billing_account = BillingAccount.find(params[:id])
  end

  # POST /billing_accounts
  # POST /billing_accounts.xml
  def create
    @billing_account = BillingAccount.new(params[:billing_account])
    #temporary  need to rewrite it
    # add method to catch CustomError
      respond_to do |format|
    begin
      @temp = @billing_account.save
      # end

        if @temp
          flash[:notice] = 'BillingAccount was successfully created.'
          format.html { redirect_to(@billing_account) }
          format.xml  { render :xml => @billing_account, :status => :created, :location => @billing_account }
        else
          format.html { render :action => "new" }
          format.xml  { render :xml => @billing_account.errors, :status => :unprocessable_entity }
        end

    rescue ZepFrog::CustomError => e
      @billing_account.errors.add_to_base "SORRY !!! will re-write"
      @billing_account.errors.add_to_base e.inspect
      format.html { render :action => "new" }
      format.xml  { render :xml => @billing_account.errors, :status => :unprocessable_entity }

    end
        end
  end

  # PUT /billing_accounts/1
  # PUT /billing_accounts/1.xml
  def update
    @billing_account = BillingAccount.find(params[:id])

    respond_to do |format|
      #temporary  need to rewrite it
      # should i allow to change existing data in authorize.net or not
      @billing_account.errors.add_to_base "SORRY UPDATE DOESN'T ALLOW in this version"
        format.html { render :action => "edit" }
        format.xml  { render :xml => @billing_account.errors, :status => :unprocessable_entity }
      
      
      # if @billing_account.update_attributes(params[:billing_account])
      #   flash[:notice] = 'BillingAccount was successfully updated.'
      #   format.html { redirect_to(@billing_account) }
      #   format.xml  { head :ok }
      # else
      #   format.html { render :action => "edit" }
      #   format.xml  { render :xml => @billing_account.errors, :status => :unprocessable_entity }
      # end
    end
  end

  # DELETE /billing_accounts/1
  # DELETE /billing_accounts/1.xml
  def destroy
    @billing_account = BillingAccount.find(params[:id])
    @billing_account.destroy

    respond_to do |format|
      format.html { redirect_to(billing_accounts_url) }
      format.xml  { head :ok }
    end
  end
end
